package com.party.controller.sys.learning;

import com.party.common.Page;
import com.party.model.*;
import com.party.service.exam.ExamAnswerService;
import com.party.service.exam.ExamService;
import com.party.service.exam.ExamTestService;
import com.party.service.study.AnswerService;
import com.party.service.study.CurriculumService;
import com.party.service.study.LearningContentService;
import com.party.service.study.TestService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/sys/test")
public class TestController {

    @Resource
    CurriculumService curriculumService;
    @Resource
    LearningContentService learningContentService;
    @Resource
    TestService testService;
    @Resource
    AnswerService answerService;

    private int pageSize = 9;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/learning/test");
        return modelAndView;
    }

    /**
     * 查询数据
     * @param req
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String title = req.getParameter("title");
        String contentTitle = req.getParameter("contentTitle");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        Test test = new Test();
        test.setTitle(title);
        test.setContentTitle(contentTitle);
        Page p = this.testService.getList(test, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount());
        map.put("tableData", p.getResult());
        return map;
    }

    /**
     * 增加
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/learning/test_add");
        List<LearningContent> contentList=learningContentService.getAll();
        modelAndView.addObject("contentList", contentList);
        return modelAndView;
    }

    /**
     * 增加数据
     * @return
     */
    @RequestMapping(value = "/addData")
    @ResponseBody
    public Map<String,Object> addData(
            @ModelAttribute("test") Test test,
            @RequestParam("data") String data,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {

        String msg="";
        Map<String, Object> map = new HashMap<String, Object>();

        if("".equals(data)){
            msg="添加失败";
            map.put("msg",msg);
            return map;
        }
        String [] dataArray=data.split("Θ");
        List<Answer> listExamAnswer=new ArrayList<Answer>();
        for(String result:dataArray){
            String[] examAnswerArray=result.split("Γ");
            Answer examAnswer=new Answer();
            examAnswer.setContent(examAnswerArray[0]);
            if("false".equals(examAnswerArray[1])){
                examAnswer.setIsright("0");
            }else{
                examAnswer.setIsright("1");
            }
            listExamAnswer.add(examAnswer);
        }

        int num=testService.addTestAndAnswer(test, listExamAnswer);
        if(num>0){
            msg="添加成功";
        }else{
            msg="添加失败";
        }
        map.put("msg",msg);
        return map;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String id=request.getParameter("id");
        String msg="";
        Test test=null;
        List<Answer> listExamAnswer=null;
        if(!"".equals(id)){
            int intId=Integer.parseInt(id);
            test=testService.getById(intId);
            listExamAnswer=answerService.getByTestId(intId);
        }

        if(test==null){
            msg="获取成功";
        }else{
            msg="获取失败";
        }

        modelAndView.addObject("test",test);
        modelAndView.addObject("listExamAnswer",listExamAnswer);
        modelAndView.setViewName("/sys/learning/test_edit");
        return modelAndView;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/editData")
    @ResponseBody
    public Map<String,Object> editData(
            @RequestParam("id") Integer testId,
            @RequestParam("title") String title,
            @RequestParam("data") String data,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {
        Test test = new Test();
        test.setId(testId);
        test.setTitle(title);
        String msg="";
        Map<String, Object> map = new HashMap<String, Object>();

        if("".equals(data)){
            msg="编辑失败";
            map.put("msg",msg);
            return map;
        }
        String [] dataArray=data.split("Θ");
        List<Answer> listExamAnswer=new ArrayList<Answer>();
        for(String result:dataArray){
            String[] examAnswerArray=result.split("Γ");
            Answer examAnswer=new Answer();
            examAnswer.setContent(examAnswerArray[0]);
            Integer id=Integer.parseInt(examAnswerArray[2]);
            if(id!=-1){
                examAnswer.setId(Integer.parseInt(examAnswerArray[2]));
            }
            if("false".equals(examAnswerArray[1])){
                examAnswer.setIsright("0");
            }else{
                examAnswer.setIsright("1");
            }
            listExamAnswer.add(examAnswer);
        }

        int num=testService.editTestAndAnswer(test,listExamAnswer);
//        examAnswerService.deleteExamByIds(deleteExamTestIds);
        if(num>0){
            msg="编辑成功";
        }else{
            msg="编辑失败";
        }
        map.put("msg",msg);

        return map;
    }


    /**
     * 删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = testService.delete(ids);
        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }
}
